//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef DEF_H_
#define DEF_H_

#pragma warning(disable: 4996 )

#include <wx/string.h>
#include <wx/colour.h>

//#include	<wx/wx.h>
//#include	<wx/socket.h>
#include	<queue>
#include	<string>
#include	<list>
#include	<vector>
#include	<map>

#if defined(LINUX) || defined(MAC)
	#define OutputDebugStr(s)
#endif

#include	"rtexception.h"

#define SWITCH_SHOW_MINIMIZED  "s"
#define SWITCH_DISABLE_SINGLE  "d"
#define AUTO_RUN_NAME wxT("routercmd")

#define PROG_VERSION			wxT("1.2.50")

enum cPROTOCOL {cPROTOCOLTelnet=0, cPROTOCOLSSH, cPROTOCOLSNMP, cPROTOCOLTR069 };

//Events
#define MESSAGE_ID_START		7777
							
//Config
#define RCMD_CFG_APP_ID			wxT("ryuho")
#define CFG_USER_VAL	 		wxT("routercmd/user")
#define CFG_PASSW_VAL	 		wxT("routercmd/password")
#define CFG_PROTO_VAL			wxT("routercmd/proto")
#define CFG_IP_VAL	 			wxT("routercmd/ip")
#define CFG_PORT_VAL 			wxT("routercmd/port")
#define CFG_PROP_VAL			wxT("routercmd/properties")
#define CFG_LANG_VAL 			wxT("routercmd/lang")
#define CFG_FILTERS_VAL			wxT("routercmd/filters")
#define CFG_MACLST_VAL			wxT("routercmd/maclist")
#define CFG_OPT_XML_VAL			wxT("routercmd/optxml")
#define CFG_PORTFORWARD_XML_VAL wxT("routercmd/portforward")
#define CFG_ROUTER_ID_VAL		wxT("routercmd/ipp")

#define CFG_USE_SSH_KEYS		wxT("routercmd/sshkeys")
#define CFG_SSH_PRV_KEY			wxT("routercmd/sshprvkey")
#define CFG_SSH_PUB_KEY			wxT("routercmd/sshpubkey")
#define CFG_SSH_PHRASE_PHR		wxT("routercmd/sshphrase")

#define CFG_WELCOME_STR			wxT("routercmd/login")
#define CFG_SHELL_STR			wxT("routercmd/shell")
#define CFG_HTTP_STR			wxT("routercmd/httpbeforeconnect")
#define CFG_HTTP_ENABLE_VAL		wxT("routercmd/enablehttpbeforeconnect")

#define CFG_ADSL_XML_VAL		wxT("routercmd/adsltweak")
#define CFG_WIRELESS_XML_VAL	wxT("routercmd/wrlstweak")

#define CFG_CHAINSEED_XML_VAL	wxT("routercmd/chainseed")
#define CFG_IP_RESOLVE_VAL		wxT("routercmd/enableipresolve")

#define CFG_PING_VAL			wxT("routercmd/ping") 


typedef struct tagCONPARAMS
{
	tagCONPARAMS():wPort(23), cProto(cPROTOCOLTelnet),
		bEnableSSHKey(false),strUsr(wxT("admin")),strPassw(wxT("Admin")),
		strLoginPrompt(wxT("Login")), strShell(wxT("sh")),
		strHTTP(wxT("/setup.cgi?todo=debug")), bHTTPEnable(false)
	{}

	wxString 		strLoginPrompt;
	wxString 		strShell;
	wxString 		strUsr;
	wxString 		strPassw;
	wxString 		strIP;
	unsigned short	wPort;
	cPROTOCOL		cProto;

	bool			bEnableSSHKey;
	wxString		strPublickey;
	wxString		strPrivkey;
	wxString		strPassphrase;

	wxString		strHTTP;
	bool			bHTTPEnable;

	wxString		strModemSettings;
	wxString		strWirelessSettings;

	bool IsValid()
	{
		return wPort && !strIP.IsEmpty() && !strUsr.IsEmpty();
	}
	
}CONPARAMS;

typedef enum cPanelType
{
	cPTNone=0,
	cPTNAT,
	cPTMem,
	cPTRoute,
	cPTCPU,
}PANELTYPE;

typedef enum cRecoveryStrategy
{
	cRCACTNothing=0,
	//cRCACTGrow,
	cRCACTFlush,
	cRCACTReboot,
	cRCACTScript
}RECOVERYSTRAT;


typedef struct tagSYSLOGMSG
{
	tagSYSLOGMSG():bSending(false) {}

	bool		bSending;
	wxString	strRaw;
	time_t		tm;
	wxString	strHost;
	int			iFacil;
	int			iPrio;
	wxString	strTag;
	wxString	strMsg;
	wxString	strTime;
}SYSLOGMSG;
typedef std::vector<SYSLOGMSG> SYSLOGMSGSTORE;
#define MAX_SYS_LOG_SIZE 1024

typedef struct tagSYSLOGACTION
{
	wxString			strPattern;
	wxString			strScriptName;
	cRecoveryStrategy	recvStrat;
}SYSLOGACTION;

typedef std::map<unsigned char, SYSLOGACTION> SYSLOGACTIONS;
#define MAX_SYSLOG_RULES	7


//thread
#define	TICK_TIME				1000 /*1 sec*/
#define IDLE_THREAD_MAX			60	/*one minute*/
#define MAX_WORK_THREADS		10
#define CHECK_WAN_ADDRESS_INT	60*60
#define CONNECT_TO_ROUTER_TIME_OUT 10000

#define MAX_TRIAL_TIME			30*24

//Port Forwarding
#define DEFAULT_AUTO_PF_LOW_BOUND 1024

//REG
#define CODE_GET_MAC_OK			1
#define CODE_GET_MAC_FAIL		0

//SCRIPT
#define CODE_SCRIPT_EXEC_OK		1
#define CODE_SCRIPT_EXEC_FAIL	0


//telnet
typedef std::string				RTSTRING;
#define SOCK_RCV_BUF_SIZE		0x1000
#define RT_BUF_SIZE				0x1000
#define RT_MAX_READ_TIMEOUT		1500
#define MAX_LOGIN_TIMEOUT		30

#define SHELL_CMD				"sh"
#define GREP_CMD				"grep"

#define TELNET_ENTER			"\n"
#define TELNET_ENTER_SEQ		"\r\n"
#define SSH_ENTER_SEQ			"\n"
#define NO_SUCH_FILE			"No such file or directory"
#define STR_WRONG_USER_OR_PASSW	"incorrect"
#define STR_TEST_CMD			wxT("echo 1C8EA58037C6")
#define STR_TEST_RES			"1C8EA58037C6"
#define STR_EXIT_CMD			wxT("exit")
#define STR_LOGOUT_CMD			wxT("logout")
#define STR_CAT_CMD				wxT("cat")

#define DEF_RT_PATH				("/proc/net/route")
#define DEF_NETFILTER_PATH		wxT("/proc/sys/net/ipv4/netfilter/")
#define DEF_IP_PATH				wxT("/proc/sys/net/ipv4/")

#define PATH_CONTRACK_GREP		wxT("/proc/slabinfo|grep ip_conntrack")
#define PATH_CONTRACK_NO_GRP	wxT("/proc/slabinfo")
#define PATH_CONTRACK_NO_GRP_VAL "ip_conntrack"


#define PATH_CONTRACK_TABLE		wxT("/proc/net/ip_conntrack")
#define PATH_CONTRACK_MAX		wxT("/proc/sys/net/ipv4/netfilter/ip_conntrack_max")
#define PATH_CONTRACK_HASH		wxT("/proc/sys/net/ipv4/netfilter/ip_conntrack_buckets")

#define PATH_CONTRACK_HASH_SET_2_6_14 wxT("/sys/module/ip_conntrack/parameters/hashsize")
#define PATH_CONTRACK_HASH_SET_2_6_20 wxT("/sys/module/nf_conntrack/parameters/hashsize")

#define CMD_MEM_TOTAL_GREP		wxT("/proc/meminfo|grep MemTotal")
#define CMD_MEM_TOTAL_NO_GRP	wxT("/proc/meminfo")
#define CMD_MEM_TOTAL_NO_GRP_VAL "MemTotal"

#define CMD_MEM_FREE_GREP		wxT("/proc/meminfo|grep MemFree")
#define CMD_MEM_FREE_NO_GRP		wxT("/proc/meminfo")
#define CMD_MEM_FREE_NO_GRP_VAL	"MemFree"

#define CMD_RT_CACHE_TOTAL		wxT("/proc/sys/net/ipv4/route/max_size")

#define CMD_RT_CACHE_CUR_GRP	wxT("/proc/slabinfo|grep ip_dst_cache")
#define CMD_RT_CACHE_CUR_NO_GRP	wxT("/proc/slabinfo")
#define CMD_RT_CACHE_CUR_NO_GRP_VAL	"ip_dst_cache"

#define CMD_CPU_TOTAL_GREP		wxT("/proc/stat|grep cpu")
#define CMD_CPU_TOTAL_NO_GRP 	wxT("/proc/stat")
#define CMD_CPU_TOTAL_NO_GRP_VAL "cpu"

#define CMD_REBOOT 				wxT("reboot")


//per interface statistics 
#define	CMD_INTERFACE_STAT		wxT("/proc/net/dev")

//wireless statistics
#define CMD_WIRELESS_STAT		wxT("/proc/net/wireless")

//network statistics
#define CMD_NET_STAT			wxT("/proc/net/netstat")

//list of known MAC
#define CMD_ARP_TABLE			wxT("/proc/net/arp")

//wireless stat
#define CMD_WLAN_MGMT_STAT		wxT("/proc/wlan/mgmt/staTable")

//ip settings
#define CMD_GET_IP_SETTINGS		wxT("ifconfig")

//router id
#define CMD_GET_ROUTER_ID		wxT("ifconfig eth0")

//router id alt
#define CMD_GET_ROUTER_ID_ALT	wxT("cat /proc/cpuinfo")

//Proc List
#define CMD_GET_PROC_LIST		wxT("echo /proc/*")

//ADSL stat
#define CMD_GET_ADSL_STATS		wxT("/proc/avalanche/avsar_modem_stats")

//Version
#define CMD_GET_VERSION			wxT("/proc/version")

//Flush routing cashe
#define CMD_FLUSH_RT_CACHE		"echo 1 > /proc/sys/net/ipv4/route/flush"

//Ping
#define CMD_PING				wxT("/bin/ping -c 3 ")

//Get Proc List
#define CMD_GET_PROCESS_LIST	wxT("ps")

//wireless proc
#define CMD_WIRELESS_PROC_STAT	wxT("/proc/wlan/mgmt/staTable")



typedef struct tagNATTIMEOUTS
{
	tagNATTIMEOUTS():stGeneric(600), stESTABLISHED(3600), stSYN_SENT(120),
		stSYN_RECV(60), stFIN_WAIT(120), stTIME_WAIT(120),  stCLOSE(10),
		stCLOSE_WAIT(60), stLAST_ACK(30), stUDP(120), stUDPStream(180), stMaxEntries(1024) {}

	size_t						stGeneric;		//ip_conntrack_generic_timeout
	size_t						stESTABLISHED;	//ip_conntrack_tcp_timeout_established
	size_t						stSYN_SENT;		//ip_conntrack_tcp_timeout_syn_sent
	size_t						stSYN_RECV;		//ip_conntrack_tcp_timeout_syn_recv
	size_t						stFIN_WAIT;		//ip_conntrack_tcp_timeout_fin_wait
	size_t						stTIME_WAIT;	//ip_conntrack_tcp_timeout_time_wait
	size_t						stCLOSE;		//ip_conntrack_tcp_timeout_close
	size_t						stCLOSE_WAIT;	//ip_conntrack_tcp_timeout_close_wait
	size_t						stLAST_ACK;		//ip_conntrack_tcp_timeout_last_ack
	size_t						stUDP;			//ip_conntrack_udp_timeout
	size_t						stUDPStream;	//ip_conntrack_udp_timeout_stream
	size_t						stMaxEntries;	///proc/sys/net/ipv4/netfilter/ip_conntrack_max
}NATTIMEOUTS;


//Status bar sizes
#define STATUS_CTRL_WIDTH 		60

#if defined(LINUX)
#define STATUS_CTRL_HEIGH 		60
#else
#define STATUS_CTRL_HEIGH 		60
#endif

#define DEF_CTRL_BORDER			5
#define GRAPH_CTRL_WIDTH 		360
#define DEF_PANEL_BORDER		1

//Command queue
#define QUEUE_MAX_SIZE			100

typedef enum cCmdCode
{
					cCmdNone=0,			/*do not send this*/
					cCmdQuit,
					cCmdTick, 			/*one per second*/
					cCmdConntracMax,
					cCmdGetSlabTable,
					cCmdMemMax,
					cCmdMemVal,
					cCmdRTCacheMax,
					cCmdRTCacheVal,
					cCmdCPU,
					cCmdScript,
					//cCmdGrow,
					//cCmdReArrange,
					cCmdConntrTableEntries,
					cCmdSysLogMessage,
					cCmdInterfaceStat,
					cCmdARPTable,
					
					cCmdGetWirelessStats,
					cCmdSetWirelessCaps,
					cCmdExecuteWirelessXML,

					cCmdGetIPConfig,
					cCmdGetRoutingTable,
					cCmdGetRouterWANAddress,
					cCmdPFTableUpdated,
					cCmdGetRouterID,
					cCmdMACBlockExecuted,
					cCmdGetTCPTimeOuts,
					cCmdGetModemStats,
					cCmdSetModemCaps,
					cCmdExecuteModemXML,
					cCmdGetVersion,
					cCmdFlushRTTable,
					cCmdPing,
					cCmdGetProcessList,
					cCmdSetDetectedWirelessMAC,
					cInternalCmdGetPerfomanceTab,
					cCMDShellCaps,
					cCMDRouterReady
}CMDCODE;

typedef enum cCmdSubCode
{
	cCmdSubCodeNone=0,			
	cCmdSubCodePortForwarding,
	cCmdSubCodeADSLTweak,
	cCmdSubCodeSyslogAutoConfig,
	cCmdSubCodeXML
}CMDSUBCODE;

typedef struct tagCOMMANDRESPONSE
{
	tagCOMMANDRESPONSE():code(cCmdNone), subCode(cCmdSubCodeNone), lData(0), lData1(0) {}
	
	CMDCODE			code;
	CMDSUBCODE		subCode;

	RTSTRING		strXML;
	RTSTRING		strData;
	unsigned long	lData;
	unsigned long	lData1;
	SYSLOGMSG		msg;
	NATTIMEOUTS		natTimeOuts;
	
}COMMANDRESPONSE;

typedef struct tagCOMMANDREQUEST
{
	tagCOMMANDREQUEST(CMDCODE cmd, CMDSUBCODE sub=cCmdSubCodeNone):code(cmd), subCode(sub) {}
	tagCOMMANDREQUEST():code(cCmdNone), subCode(cCmdSubCodeNone) 		 {}
	
	CMDCODE			code;
	CMDSUBCODE		subCode;
	wxString		strScript;
	//PANELTYPE		type; //needed for cCmdGrow
}COMMANDREQUEST;

//GUI bindings
struct IGUIBind
{
	virtual void 			SetMaxVal(unsigned int nVal)			{};
	virtual void 			SetVal(unsigned int nVal)				{};
	virtual void 			SetWarningLevel(unsigned char btVal)	{};
	virtual void 			SetAlarmLevel(unsigned char btVal)		{};
	virtual void 			SetVal(const wxString& str)				{};
	virtual void			OnResponse(const COMMANDRESPONSE& resp) {};
	virtual void			Reset()									{};
};

typedef struct tagPropEntry
{
	friend class  CPropertyStorage;
	
	tagPropEntry():btWarningLevel(66), btAlarmLevel(90),
		onWarning(cRCACTFlush), onAlarm(cRCACTReboot),
		tLastChange(0), bEnableRepair(false) {}
		
	bool operator!=(const tagPropEntry& entry)
	{
		return	btWarningLevel!=entry.btWarningLevel ||
				btAlarmLevel!=entry.btAlarmLevel ||
				onWarning!=entry.onWarning ||
				onAlarm!=entry.onAlarm ||
				onWarningScript!=entry.onWarningScript ||
				onAlarmScript!=entry.onAlarmScript ||
				tLastChange!=entry.tLastChange ||
				bEnableRepair!=entry.bEnableRepair;
	}
	
	unsigned char btWarningLevel;
	unsigned char btAlarmLevel;
	RECOVERYSTRAT onWarning;
	RECOVERYSTRAT onAlarm;
	wxString	  onWarningScript;
	wxString	  onAlarmScript;

	bool		  bEnableRepair;	
	
protected:	
	time_t		  tLastChange;
}PROPENTRY;

struct IPropStorage
{
	virtual bool IsChanged(PANELTYPE pt, const PROPENTRY& pp)=0;
	virtual void GetProperty(PANELTYPE pt, PROPENTRY& pp)=0;
	virtual void SetProperty(PANELTYPE pt, const PROPENTRY& pp)=0;
};

#define DEF_STABLE_TICK	10 /*number of tick for start action*/

typedef struct tagGUIBIND
{
	tagGUIBIND():bEnabled(true), bOneShort(false), codeMaxVal(cCmdNone), codeVal(cCmdNone), tLastUpdate(0),
		lLevelCount(0),  bWarningFaired(false), plType(cPTNone), pStorage(NULL),
		lLevelMaxVal(0), lLevelCurVal(0), pBind(NULL), tUpdateDelay(0)  {}

	bool			bEnabled;
	bool			bOneShort; //One short support 
	CMDCODE			codeMaxVal;
	CMDCODE			codeVal;
	time_t			tLastUpdate;
	time_t			tUpdateDelay;

	unsigned long	lLevelCount;
	bool			bWarningFaired;

	//storage
	PANELTYPE		plType;
	IPropStorage*	pStorage;
	PROPENTRY		dataEntry;

	unsigned long GetMaxVal() { return  lLevelMaxVal; }
	void SetMaxVal(unsigned long val)
	{
		lLevelMaxVal = val;
		if ( pBind ) pBind->SetMaxVal(val);
	}

	unsigned long GetVal() { return  lLevelCurVal; }
	void SetVal(unsigned long val)
	{
		lLevelCurVal = val;
		if ( pBind ) pBind->SetVal(val);
	}


	void SetTxtVal(const wxString& str)
	{
		if ( pBind ) pBind->SetVal(str);
	}

	void SetWarningLevel(unsigned char btVal)
	{
		if ( pBind ) pBind->SetWarningLevel(btVal);
	}

	void SetAlarmLevel(unsigned char btVal)
	{
		if ( pBind ) pBind->SetAlarmLevel(btVal);
	}

	void Reset()
	{
		if ( pBind ) pBind->Reset();
	}

	void SetBinding(IGUIBind* p) { pBind=p;		 }
	IGUIBind* GetBinding()		 { return pBind; }

protected:
	unsigned long	lLevelMaxVal;
	unsigned long	lLevelCurVal;
	IGUIBind*		pBind;
}GUIBIND;

//Log
enum cLogColumns  { cLogColumnTime=0,cLogColumnSrc, cLogColumnEvent };
#define MAX_LOG_RECORDS 1000
typedef struct tagLOGENTRY
{
	tagLOGENTRY():bStatus(false), bSent(false), bSending(false), bDoNotSave(false)  {}
	bool			bStatus;
	bool			bDoNotSave;
	bool			bSent;
	bool			bSending;
	time_t			tEvent;
	PANELTYPE		plType;
	RECOVERYSTRAT	strat;
	wxString		strCustomEvent;
}LOGENTRY;

typedef std::vector<LOGENTRY>	APPLOG;

//Menu
#define HIGH_UPDATE_SPEED	1
#define NORMAL_UPDATE_SPEED 10
#define LOW_UPDATE_SPEED	30

//Stat
typedef std::map<wxString, unsigned int> STATMAP;

typedef struct tagCONNTRACENTRY
{
	wxString strRaw;
	wxString strProto;
	wxString strTTL;
	wxString strStatus;
	wxString strSrcCountry;
	wxString strSrc;
	wxString strSrcName;
	wxString strSrcPort;
	wxString strDstCountry;
	wxString strDst;
	wxString strDstName;
	wxString strDstPort;
}CONNTRACENTRY;

//Search
enum cSearchType { cSearchTypeString, cSearchTypeNumeric, cSearchDateTime };

typedef struct tagSEARCHVAL
{
	cSearchType cType;
	wxString	strData;
	size_t		stDigitData;
	time_t	    timeData;

}SEARCHVAL;

#define SEARCH_FIELD_ANY 0xFF
#define SEARCH_NOT_FOUND -1

typedef struct tagSEARCHFIELD
{
	cSearchType 	cType;
	unsigned int	iFieldID;
	wxString		strLabel;
}SEARCHFIELD;

enum cSearchOwnerID { cSearchOwnerIDLog=0, cSearchOwnerIDConntrac, cSearchOwnerIDSysLog, cSearchOwnerIDRtrUsers, cSearchOwnerIDPortForwarding, cSearchOwnerIDWireless  };

struct ISearchProvider
{
	virtual void 			GetSearchFields(SEARCHFIELD*& sf, size_t& sz)=0;
	virtual int 			Search(const wxString& val, unsigned int iFieldID, size_t posStart)=0;
	virtual cSearchOwnerID	GetID()=0;
	virtual void			OnFilterChanged()=0;
	virtual bool			HasFilter() {return true;}
};

enum cFilterRule { cFilterRuleAny=0 ,cFilterRuleBigger, cFilterRuleBiggerOrEq,cFilterRuleEq, cFilterRuleNotEq,
	cFilterRuleSmaller, cFilterRuleSmallerOrEq, cFilterRuleRegExp  };

typedef struct tagFILTERFIELDRULE
{
	unsigned int	iFieldID;
	cFilterRule		rule;
	wxString		strVal;
}FILTERFIELDRULE;

typedef std::vector<FILTERFIELDRULE> FIELDRULESET;

typedef struct tagFILTER
{
	tagFILTER():bActive(false), bSysDef(false) {}

	FIELDRULESET	filterDef;
	wxString		strFilterName;
	bool			bActive;
	bool			bSysDef;
}FILTER;

typedef std::vector<FILTER> FILTERS;

#define NOT_FILTERED (void*)NULL
#define FILTERED	 (void*)1

typedef std::vector<std::string> 	TABLEHEADERS;
typedef std::vector<std::string> 	TABLEROW;
typedef std::vector<TABLEROW> 		TABLEROWS;
	
typedef struct tagPROCTABLEWITHHEADER
{
	size_t size() { return rows.size();  }
	bool empty()  { return rows.empty(); }

	TABLEROWS 	 rows;
	TABLEHEADERS headers;
}PROCTABLEWITHHEADER;


typedef struct tagNAMEDVAL
{
	RTSTRING strLabel;
	RTSTRING strVal;
}NAMEDVAL;
typedef std::vector<NAMEDVAL> NAMEDVALS;


///////////////////////////////////////////////////////////////////////
//Mac blocker
//TOO: add block condition
enum cMACBlockRuleId   { cMACBlockRuleIdWL=0, cMACBlockRuleIdBL };
typedef std::vector<std::string> TEXTLIST;

typedef struct tagMACENTRY
{
	tagMACENTRY(bool bDefaultVal=true)
	{
		bEnableSched = false;
		arrSched.resize(7*24, bDefaultVal); 
	}

	RTSTRING			SchedToString() const;
	void				SchedFromString(const RTSTRING& str);
	bool				IsBlockedByScheduler() const;

	RTSTRING			strMAC;
	bool				bEnableSched;
	std::vector<bool>	arrSched;
}MACENTRY;
typedef std::vector<MACENTRY> MACLIST;

typedef struct tagMACBLOCK
{
	bool			blockOthers; //white list only
	MACLIST			list;
	cMACBlockRuleId id;
}MACBLOCK;

struct IGUISupport
{
	virtual bool			CanSave()	{ return false;		}
	virtual wxString		Save()		{ return wxT("");	}

	virtual bool			CanClear()	{ return false;		}
	virtual void			Clear()		{ 	}

	virtual bool			CanShowAdvanced()	{ return false;		}
	virtual bool			IsAdvancedShown()	{ return false;		}
	virtual void			ShowAdvanced(bool bShow=true){ 	}
};

///////////////////////////////////////////////////////////////////////
//Advanced optimization script
typedef struct tagSTRINGPAIR
{
	wxString strLabel;
	wxString strVal;
}STRINGPAIR;
typedef std::vector<STRINGPAIR> STRINGPAIRLIST;

///////////////////////////////////////////////////////////////////////
//Port forwarding
enum cPortFrorwardProto { cPortFrorwardProtoAny=0, cPortFrorwardProtoTCPIP,cPortFrorwardProtoUDP };

typedef struct tagPORTFORWARDRULE
{
	tagPORTFORWARDRULE():bAutoRule(false), bAutoMustBeDeleted(false), bRuleInProgress(false),
		bEnabled(true), proto(cPortFrorwardProtoAny), wPortFrom(0), wPortFromTo(0), wPortTo(0)  {}

	bool				bAutoRule;
	bool				bAutoMustBeDeleted;
	bool				bRuleInProgress;
	bool				bEnabled;
	wxString			strLabel;
	wxString			strProcName;
	cPortFrorwardProto  proto;
	wxString			strDst;
	unsigned short		wPortFrom;
	unsigned short		wPortFromTo;
	unsigned short		wPortTo;
}PORTFORWARDRULE;
typedef std::vector<PORTFORWARDRULE> PORTFORWARDLIST;

typedef struct tagPROTOENTRY
{
	cPortFrorwardProto	proto;
	wxString			strIP;
	unsigned short		wPort;
	wxString			strOwnerName;
	wxString			strProcName;
}PROTOENTRY;
typedef std::vector<PROTOENTRY> PROTOLIST;

struct CGraphSize
{
	virtual int GetGraphLeftWidth()=0;
	virtual int GetGraphWidth()=0;
	virtual int GetGraphColWidth()=0;
};


//////////////////////////////////////////////////////////////////////////
//Wireless
#define WRLS_XML_DOC_TAG				"wireless"
#define WRLS_XML_TAG_ASSOC_LST			"assoclist"
#define WRLS_XML_TAG_ENTRY				"entry"
#define WRLS_XML_TAG_STATUS				"status"

#define WRLS_XML_ATTR_CLNT_RATESET		"rateset"
#define WRLS_XML_ATTR_CLNT_IDLE			"idle"
#define WRLS_XML_ATTR_CLNT_INNETW		"in_network"
#define WRLS_XML_ATTR_CLNT_STATE		"state"
#define WRLS_XML_ATTR_CLNT_TX			"tx"		
#define WRLS_XML_ATTR_CLNT_TXFAIL		"tx_failures"
#define WRLS_XML_ATTR_CLNT_RX_UCAST		"rx_ucast"
#define WRLS_XML_ATTR_CLNT_RX_MBCAST	"rx_mbcast"
#define WRLS_XML_ATTR_CLNT_TX_RATE		"tx_rate"
#define WRLS_XML_ATTR_CLNT_RX_RATE		"rx_rate"
#define WRLS_XML_ATTR_CLNT_SIGN_STRNGTH "signal"

#define WRLS_XML_ATTR_WPA_AUTH			"wpa_auth"
#define WRLS_XML_ATTR_WSEC				"wsec"
#define WRLS_XML_ATTR_WEPSTATUS			"wepstatus"
#define WRLS_XML_ATTR_TXPWR				"txpwr"
#define WRLS_XML_ATTR_PWRPERC			"pwr_percent"
#define WRLS_XML_ATTR_ISUP				"isup"
#define WRLS_XML_ATTR_NOISE				"noise"
#define WRLS_XML_ATTR_RADIO				"radio"
#define WRLS_XML_ATTR_RATE				"rate"
//////////////////////////////////////////////////////////////////////////

#define DEF_SYSLOG_PORT					514

typedef struct tagZONERANGE
{
	int			nFrom;
	int			nTo;
	wxColor		color; 
}ZONERANGE;
typedef std::list<ZONERANGE>		ZONELIST;
typedef std::vector<unsigned int>	LINEVALS;

//////////////////////////////////////////////////////////////////////////
#define ADSL_STAT_TAG				"stat"
#define ADSL_LINK_POWER_STATE_ATTR "link_pw_state"
#define ADSL_MODE_ATTR				"mode"
#define ADSL_MODE_SEL_ATTR			"selected_mode"
#define ADSL_CHANEL_ATTR			"channel"
#define ADSL_TRELLIS_ATTR			"trellis"
#define ADSL_BITSWAP_ATTR			"bitswap"
#define ADSL_LINE_STATUS_ATTR		"status"
#define ADSL_TRAINING_STATUS_ATTR	"training_status"
#define ADSL_D_SNR_ATTR				"dsnr"
#define ADSL_U_SNR_ATTR				"usnr"
#define ADSL_D_ATTN_ATTR			"dattn"
#define ADSL_U_ATTN_ATTR			"uattn"
#define ADSL_D_MAXRATE_ATTR			"dmaxrate"
#define ADSL_U_MAXRATE_ATTR			"umaxrate"
#define ADSL_D_CONRATE_ATTR			"dconrate"
#define ADSL_U_CONRATE_ATTR			"uconrate"
#define ADSL_D_PWR_ATTR				"dpower"
#define ADSL_U_PWR_ATTR				"upower"
#define ADSL_DISC_COUNT_ATTR		"dsc_cnt"
#define ADSL_ISUP_ATTR				"isup"

#define ADSL_QLN_BIT_TAG			"qln"

#define ADSL_BITS_U_TAG				"u_bits"
#define ADSL_BITS_D_TAG				"d_bits"

#define ADSL_SNR1_BIT_TAG			"snr_bits1"
#define ADSL_SNR2_BIT_TAG			"snr_bits2"
#define ADSL_SNR3_BIT_TAG			"snr_bits3"

#define ADSL_TONE_ATTR				"tone"
#define ADSL_TONE_ID_ATTR			"id"
#define ADSL_TONE_VAL_ATTR			"val"

#define ADSL_ESEC_ERR_ATTR			"esec_err"
#define ADSL_CRC_ERR_ATTR			"crc_err"
#define ADSL_LOF_ERR_ATTR			"lof_err"
#define ADSL_LOS_ERR_ATTR			"los_err"
#define ADSL_SEF_ERR_ATTR			"sef_err"
#define ADSL_INIT_ERR_ATTR			"init_err"

#define ADSL_U_SF_ERR_ATTR			"u_sf_err"
#define ADSL_U_SFE_ERR_ATTR			"u_sfe_err"
#define ADSL_U_CRC_ERR_ATTR			"u_crc_err"
#define ADSL_U_FEC_ERR_ATTR			"u_fec_err"
#define ADSL_U_NCD_ERR_ATTR			"u_ncd_err"
#define ADSL_U_LCD_ERR_ATTR			"u_lcd_err"
#define ADSL_U_OCD_ERR_ATTR			"d_ocd_err"
#define ADSL_U_HEC_ERR_ATTR			"u_hec_err"

#define ADSL_D_SF_ERR_ATTR			"d_sf_err"
#define ADSL_D_SFE_ERR_ATTR			"d_sfe_err"
#define ADSL_D_CRC_ERR_ATTR			"d_crc_err"
#define ADSL_D_FEC_ERR_ATTR			"d_fec_err"
#define ADSL_D_NCD_ERR_ATTR			"d_ncd_err"
#define ADSL_D_LCD_ERR_ATTR			"d_lcd_err"
#define ADSL_D_OCD_ERR_ATTR			"d_ocd_err"
#define ADSL_D_HEC_ERR_ATTR			"d_hec_err"

//ADSL caps
#define ADSL_CAPS					"adslui"
#define XMLUI_BLOCK_TAG				"block"
#define XMLUI_ELEMENT_BLOCK_ID_ATTR "block_id"
#define XMLUI_BLOCK_NAME_ATR		"name"
#define XMLUI_UI_TAG				"ui"
#define XMLUI_TYPE_ATR				"type"
#define XMLUI_UI_ATTR_RADIO			"radio"
#define XMLUI_UI_ATTR_CHECK			"check"
#define XMLUI_UI_ATTR_BUTTON		"button"
#define XMLUI_UI_ATTR_BTN_APP_BLOCK	"btn_apply_block"
#define XMLUI_UI_ATTR_LINE			"line"
#define XMLUI_UI_ATTR_INPUT			"input"
#define XMLUI_UI_ATTR_INPUT_TYPE	"input_type"
#define XMLUI_UI_ATTR_INPUT_TYPE_D	"dec"
#define XMLUI_UI_ATTR_INPUT_TYPE_S	"str"
#define XMLUI_UI_ATTR_INPUT_DEF		"input_def"
#define XMLUI_UI_ATTR_INPUT_FROM	"input_from"
#define XMLUI_UI_ATTR_INPUT_TO		"input_to"
#define XMLUI_VAL_SET_ATTR			"value"
#define XMLUI_VAL_ATTR				"value_from"
#define XMLUI_VAL_CMD_ATTR			"cmd"
#define XMLUI_VAL_CMD_SEP_ATTR		"cmd_sep"
#define XMLUI_VAL_TO_ATTR			"value_to"
#define XMLUI_VAL_TO_ON_ATTR		"value_to_on"
#define XMLUI_VAL_TO_OFF_ATTR		"value_to_off"
#define XMLUI_ELEMENT_ID_ATTR		"id"
#define XMLUI_ELEMENT_LOOKUP_ID_ATTR "stat_id" /*if stat has ID in different form*/
#define XMLUI_MODE_NAME_ATTR		"name"
#define XMLUI_MODE_DESC_ATTR		"desc"

//Script 
#define XMLGUI_EXEC_EL				"exec"
#define XMLGUI_EXEC_ITEM_EL			"exec_item"
#define XMLGUI_EXEC_ATTR_STARTUP	"startup"

struct IGUIXMLExecutor
{
	virtual void SaveExecuteXML( const std::string& strXML ) =0;
	virtual std::string LoadExecuteXML() =0;
	virtual void ExecuteXML( const std::string& strXML ) =0;
};

struct ILoggable
{
	virtual wxString GetLog()=0;
};


//Wireless caps
#define WIRELESS_CAPS					"wirelessui"

#define STATUS_LOG_DISABLED				-1
#define UNLIMITED_SIZE					0

enum cTabPageID {	cTabIDPerfomance=0, cTabIDConnections, cTabIDSysLog, cTabIDBandwidth, cTabIDUsers,
					cTabIDPortForward, cTabIDReport, cTabIDADSL, cTabIDWireless, cTabIDPing };

struct ITabedPageUI
{
	virtual cTabPageID GetTabID()=0;

	//Time selector in view menu
	virtual bool	IsTimeSelectorSupported()		{ return false;	}
	virtual time_t	GetTimeInterval()				{ return 0;		}
	virtual void	SetTimeInterval(size_t tInt)	{				}
};

enum cPingStatus		{ cPingActive, cPingDisabled };
enum cPingFailedAction	{ cPingActionNone=0, cPingActionReboot, cPingActionReconect, cPingActionScript, cPingActionRun  };

typedef struct tagPINGRESULT
{
	tagPINGRESULT()
	{
		//Defaults
		bDispatched = false;
		bPingResult = false;
		stPingTime = stPingJitter =0;
		tEvent = 0;
	}

	//Ping result
	bool				bDispatched; //process only if bDispatched==false
	bool				bPingResult;
	size_t				stPingTime;
	size_t				stPingJitter;
	time_t				tEvent;
}PINGRESULT;

//Ping
typedef struct tagPingData
{
	tagPingData()
	{
		//Defaults
		stInterval = 30;
		stToActionCnt = 3;
		action = cPingActionNone;
		cStatus = cPingActive;
		stPingFailedCnt = 0;
		tLastFailed = -1;
		tLastPinged = 0;
		stPingFailuresToAction=0;
	}

	wxString			strHost;
	size_t				stInterval;
	size_t				stToActionCnt;
	cPingFailedAction	action;
	wxString			strPathToFile;
	wxColor				color;
	cPingStatus			cStatus;

	//Runtime only
	size_t				stPingFailuresToAction;
	size_t				stPingFailedCnt;
	time_t				tLastFailed;
	time_t				tLastPinged;
	PINGRESULT			result;
}PingData;

typedef std::vector<PingData> PINGDATALIST;


/////////////////////////////////////////////////////////////////
//State stats
#define STATE_NAT_EL			"nat"
#define STATE_RT_EL				"rt"
#define STATE_MEM_EL			"mem"
#define STATE_CPU_EL			"cpu"
#define STATE_BOOT_CNT			"boot_cnt"
#define STATE_REPAIR_CNT		"repair_cnt"
#define STATE_SIZECHANGE_CNT	"sizechange_cnt"
#define STATE_TOTAL_CNT			"total_cnt"
#define STATE_USED_CNT			"used_cnt"
#define STATE_LEAK_CNT			"leaked"
#define STATE_USAGE				"usage"
#define STATE_FREE				"free"
#define STATE_ALLOCTD_CNT		"alloc_cnt"

/////////////////////////////////////////////////////////////////
//Shell
#define SHELL_CAPS_EL				"shell"
#define GLOBAL_NAME_ATR				"name"
#define GLOBAL_RECV_ATR				"recv"
#define GLOBAL_TRNSM_ATR			"transm"
#define SHELL_MAC_FILTER			"mac_filter"
#define SHELL_PING					"ping"
#define SHELL_PORT_FORWARD			"port_forward"
#define SHELL_BANDWIDTH				"bandwidth"
#define SHELL_CONNECTIONS			"con_list"
#define SHELL_TCPNAT_SETTNGS		"TCPNAT_settings"
#define SHELL_PROCKILL				"proc_kill"
#define SHELL_SYSLOGCONFIG			"syslog_config"
#define SHELL_KERNELMOD				"kernel_mod"
#define SHELL_NAT_USAGE				"nat_usage"
#define SHELL_MEM_USAGE				"mem_usage"
#define SHELL_CPU_USAGE				"cpu_usage"
#define SHELL_RT_USAGE				"rt_usage"

#define XML_SCRIPT_EL				"script"
#define GLOBAL_TYPE_ATR				"type"
#define GLOBAL_ENTRY_EL				"entry"
#define GLOBAL_VALUE_ATR			"val"
#define GLOBAL_LOG_ATR				"log"

//MAC
#define SCRIPT_WHITE_LST_EL			"white"
#define GLOBAL_MAC_ATTR				"mac"
#define GLOBAL_ACTION_ATTR			"action"
#define GLOBAL_ENABLE_VAL			"enable"
#define GLOBAL_DISABLE_VAL			"disable"
#define GLOBAL_ANY_VAL				"*"

#define SCRIPT_PORT_FROM_ATTR		"from_port"
#define SCRIPT_PORT_FROM_TO_ATTR	"from_port_to"
#define SCRIPT_DST_ATTR				"to"
#define SCRIPT_DST_PORT_ATTR		"to_port"

#define GLOBAL_IP_ATTR				"ip"
#define GLOBAL_PORT_ATTR			"port"
#define GLOBAL_LEVEL_ATTR			"level"

#define SHELL_SCRIPT_FORMAT			"script_format"
#define SCRIPT_FORMAT_NATIVE_VAL	"native"
#define SCRIPT_FORMAT_XML_VAL		"xml"

//CONLIST
#define CONLIST_ROOT_EL				"conlist"
#define CONLIST_PROTO_ATTR			"proto"
#define CONLIST_PROTO_TCP_VAL		"tcp"
#define CONLIST_PROTO_UDP_VAL		"udp"
#define CONLIST_PROTO_ICMP_VAL		"icmp"
#define CONLIST_PROTO_UNK_VAL		"unknown"

#define CONLIST_DST_IP_ATTR			"dst_ip"
#define CONLIST_DST_PORT_ATTR		"dst_port"
#define CONLIST_SRC_IP_ATTR			"src_ip"
#define CONLIST_SRC_PORT_ATTR		"src_port"
#define CONLIST_TTL_ATTR			"ttl"
#define CONLIST_STATUS_ATTR			"status"

//Interface stat
#define INTSTAT_ROOT_EL				"intstat"
#define ARP_ROOT_EL					"arp"

#define BAD_COLOR					wxColor(255,236,233)
#define FAIR_COLOR					wxColor(255,254,222)
#define GOOD_COLOR					wxColor(242,254,233)
#define EXCELENT_COLOR				wxColor(220,246,206)

#endif /*DEF_H_*/
